/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "MainWorld.h"
#include "InputSystem.h"
#include "Application.h"
#include "CameraObject.h"

namespace PQGame
{
	const Ogre::String MainWorld::DATA_PATH="data/world/Sinbad.zip";
	const Ogre::String MainWorld::GROUP_NAME="group/env";

	MainWorld::MainWorld(void)
		: rightMouseDown(false),character(0)
	{
	}

	MainWorld::~MainWorld(void)
	{
	}

	MainWorld &MainWorld::getSingleton()
	{
		static MainWorld world;
		return world;
	}

	void MainWorld::init(PQEngine::SceneObject* sceneObject)
	{
		_sceneObject=sceneObject;

		initResource();
		setupScene();
		setupGUI();
		initInputListener();

		createMainCharacter();
	}

	void MainWorld::initResource()
	{
		Ogre::ResourceGroupManager::getSingleton().addResourceLocation(DATA_PATH,"Zip",GROUP_NAME);
		Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup(GROUP_NAME);

	}

	void MainWorld::createMainCharacter()
	{
		character=new SinbadCharacter(_sceneObject);
		character->setPostion(700,0,700);
	}

	void MainWorld::setupGUI()
	{
		
	}

	void MainWorld::initInputListener()
	{
		InputSystem::getSingleton().addKeyListener(this,"MainWorldKeyListener");
		InputSystem::getSingleton().addMouseListener(this,"MainWorldMouseListener");
	}

	void MainWorld::setupScene()
	{
		
	}


	float MainWorld::getTerrainHeight(const float x, const float z)
	{
		return 0;
	}

	void MainWorld::update(float deltaT)
	{
		_sceneObject->update(deltaT);
		if(character!=NULL){
			character->update(deltaT);
		}

	}

	bool MainWorld::keyPressed(const OIS::KeyEvent &e)
	{
		switch(e.key){
			case OIS::KC_ESCAPE:
				Application::getSingleton().shutdown();
				return false;
		}

		if(character!=NULL){
			character->injectKeyDown(e);
		}
		return true;
	}

	bool MainWorld::keyReleased( const OIS::KeyEvent &e)
	{
		if(character!=NULL){
			character->injectKeyUp(e);
		}
		return true;
	}

	bool MainWorld::mouseMoved( const OIS::MouseEvent &e)
	{
		if(character!=NULL){
			character->injectMouseMove(e);
		}
		return true;
	}

	bool MainWorld::mousePressed( const OIS::MouseEvent &e, OIS::MouseButtonID id)
	{
		if(character!=NULL){
			character->injectMouseDown(e,id);
		}
		return true;
	}
	
	bool MainWorld::mouseReleased( const OIS::MouseEvent &e, OIS::MouseButtonID id )
	{
		rightMouseDown=false;
		return true;
	}
}
